package cn.estore.util;

import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.utils.Utils;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.Security;
import java.util.Properties;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * DES加密和解密工具,可以对字符串进行加密和解密操作  。 
 */
public class Encrypt {

  public static final String ALGORITHM = "DES";
  private static final String TRANSFORMATION = "DES/ECB/PKCS5Padding";
  
  private static final String ENCODING = StandardCharsets.UTF_8.name();
  /** 字符串默认键值     */
  private static String strDefaultKey = "national";

  private Properties properties = null;
  private CryptoCipher encryptCipher = null;

  /** 解密工具     */
  private CryptoCipher decryptCipher = null;

  /**  
   * 默认构造方法，使用默认密钥  
   *   
   * @throws Exception  
   */
  public Encrypt() throws Exception {
    this(strDefaultKey);
  }

  /**  
   * 指定密钥构造方法  
   *   
   * @param strKey  
   *            指定的密钥  
   * @throws Exception  
   */
  public Encrypt(String strKey) throws Exception {
//    properties = new Properties();
//    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CryptoCipherFactory.CipherProvider.JCE.getClassName());
//
//    SecretKeySpec keySpec = new SecretKeySpec(strKey.getBytes(ENCODING), ALGORITHM);
//    encryptCipher = Utils.getCipherInstance(TRANSFORMATION, properties);
//    encryptCipher.init(Cipher.ENCRYPT_MODE, keySpec);

//    decryptcipher = utils.getciphejrinstance(algorithm, utils.getdefaultproperties());
//    decryptcipher.init(cipher.decrypt_mode, key, decryptcipher.getalgorithm());
  }

  /**  
   * 加密字符串  
   *   
   * @param strIn  
   *            需加密的字符串  
   * @return 加密后的字符串  
   * @throws Exception  
   */
  public String encrypt(String strIn) throws Exception {
    return strIn;
  }


  /**  
   * 解密字符串  
   *   
   * @param strIn  
   *            需解密的字符串  
   * @return 解密后的字符串  
   * @throws Exception  
   */
  public String decrypt(String strIn) throws Exception {
    return strIn;
  }

  /**
   * main方法  。
   * @param args
   */

  public static void main(String[] args) {
    try {
      String test = "2465a19cc9dfe758";
      Encrypt des = new Encrypt("njcit");//自定义密钥   
   //   System.out.println("加密前的字符：" + test);
    //  System.out.println("加密后的字符：" + des.encrypt(test));
    //  System.out.println("解密后的字符：" + des.decrypt(des.encrypt(test)));      
      System.out.println("加密后的字符：" + test);
      System.out.println("解密后的字符：" + des.decrypt(test));      
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  
}